package org.cbyyx.tc.controller;

import lombok.RequiredArgsConstructor;
import org.cbyyx.tc.annotation.AuthCheck;
import org.cbyyx.tc.common.BaseResponse;
import org.cbyyx.tc.common.ResultUtils;
import org.cbyyx.tc.constant.UserConstant;
import org.cbyyx.tc.enums.ErrorCode;
import org.cbyyx.tc.exception.ThrowUtils;
import org.cbyyx.tc.model.dto.categoryRequest.CategoryAddRequest;
import org.cbyyx.tc.model.dto.categoryRequest.CategoryQueryRequest;
import org.cbyyx.tc.model.dto.categoryRequest.CategoryUpdateRequest;
import org.cbyyx.tc.model.vo.categoryResponse.CategoryVO;
import org.cbyyx.tc.service.ProductCategoryService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 分类接口
 *
 * @author cbyyx
 */
@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
public class ProductCategoryController {
    private final ProductCategoryService productCategoryService;

    /**
     * 添加分类
     *
     * @param request 请求参数
     * @return 结果
     */
    @PostMapping
    @AuthCheck(role = UserConstant.ADMIN)
    public BaseResponse<Boolean> addCategory(@RequestBody @Valid CategoryAddRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        return ResultUtils.success(productCategoryService.addCategory(request));
    }

    /**
     * 更新分类信息
     * @param request 分类更新请求参数，包含分类ID和需要更新的字段
     * @return 更新结果，true表示成功
     */
    @PutMapping
    @AuthCheck(role = UserConstant.ADMIN)
    public BaseResponse<Boolean> updateCategory(@RequestBody @Valid CategoryUpdateRequest request) {
        ThrowUtils.throwIf(request == null || request.getId() == null, ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        return ResultUtils.success(productCategoryService.updateCategory(request));
    }

    /**
     * 删除分类
     * @param id 要删除的分类ID
     * @return 删除结果，true表示成功
     */
    @DeleteMapping("/{id}")
    @AuthCheck(role = UserConstant.ADMIN)
    public BaseResponse<Boolean> deleteCategory(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "分类ID无效");
        return ResultUtils.success(productCategoryService.deleteCategory(id));
    }

    /**
     * 获取分类详情
     * @param id 要查询的分类ID
     * @return 分类详情视图对象
     */
    @GetMapping("/{id}")
    public BaseResponse<CategoryVO> getCategory(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ErrorCode.PARAMS_ERROR, "分类ID无效");
        return ResultUtils.success(productCategoryService.getCategory(id));
    }

    /**
     * 查询分类列表
     * @param request 分类查询条件，包含分页参数和筛选条件
     * @return 分类列表
     */
    @GetMapping("/list")
    public BaseResponse<List<CategoryVO>> listCategories(CategoryQueryRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        return ResultUtils.success(productCategoryService.listCategories(request));
    }

    /**
     * 查询子分类列表
     * @param parentId 父分类ID
     * @return 子分类分页列表
     */
    @GetMapping("/children/{parentId}")
    public BaseResponse<List<CategoryVO>> listChildCategories(@PathVariable Long parentId) {
        ThrowUtils.throwIf(parentId == null || parentId < 0, ErrorCode.PARAMS_ERROR, "父分类ID无效");
        return ResultUtils.success(productCategoryService.listChildCategories(parentId));
    }
}